package Model.xml.protocols;

import Model.StatusMessage;
import Model.User;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

/**
 * 
 * @author pieterc
 */
public class StatusNetXML extends ProtocolXML {

    private StatusMessage sm;
    private User u;
    private boolean handlingUser = false;
    private String protocolName;

    public StatusNetXML(String protocolName) {
        this.protocolName = protocolName;
    }

    @Override
    protected String getStatusTagName() {
        return "status";
    }

    @Override
    protected String getUserTagName() {
        return "user";
    }

    @Override
    protected void startUser() {
        handlingUser = true;
        u = new User(protocolName);
    }

    @Override
    protected void startStatus() {
        sm = new StatusMessage();
    }

    @Override
    protected void handleElement(String elementName, String content) {
        if (handlingUser) {
            //User data
            if (elementName.equals("name")) {
                u.setRealname(content);
            } else if (elementName.equals("screen_name")) {
                u.setNickname(content);
            } else if (elementName.equals("profile_image_url")) {
                u.setAvatar(content);
            }
        } else {
            //Status Message
            if (elementName.equals("statusnet:html")) {
                sm.setText(content);
            } else if (elementName.equals("created_at")) {
                //Example: Wed Jul 14 09:04:47 +0000 2010
                SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
                sm.setTime(sdf.parse(content, new ParsePosition(0)));
            } else if (elementName.equals("id")) {
                sm.setIdentification(content + "@identi.ca");//TODO
            }
        }
    }

    @Override
    protected StatusMessage getFinalStatusMessage() {
        return sm;
    }

    @Override
    protected void endUser() {
        sm.setUser(u);
        handlingUser = false;
    }
}
